package com.map.view;

import android.widget.*;
import android.content.*;
import android.support.v7.widget.*;
import android.view.*;

import com.map.ui.R;
import com.xl.game.tool.*;
import java.lang.reflect.*;
import com.xl.game.math.*;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ColorDrawable;
import android.content.res.ColorStateList;
import android.os.Build;
import android.support.v7.app.AlertDialog;

import android.content.res.TypedArray;
import android.view.inputmethod.EditorInfo;


/*
设置功能控件
点击设置后应用到静态变量中
必须为静态变量才能用
*/
public class PreferenceView extends LinearLayoutCompat
{
    int headColor;
    //TextView text_head;

    public PreferenceView(Context context)
    {
        super(context);
        initView();
    }

    private void initView()
    {
        Context context = getContext();
        this.setOrientation(LinearLayout.VERTICAL);
        headColor = getColorAccent();
        setShowDividers(SHOW_DIVIDER_MIDDLE);
    }

    //设置标签头
    public void setHead(String headText)
    {
        addHead(headText);
    }

    //添加标签头
    public void addHead(String headText)
    {
        TextView text_head = new TextView(getContext());
        text_head.setTextSize(15);
        text_head.setTextColor(headColor);
        text_head.setTag("colorAccent");
        text_head.setPadding(DisplayUtil.dip2px(getContext(),16),DisplayUtil.dip2px(getContext(),32),DisplayUtil.dip2px(getContext(),16),DisplayUtil.dip2px(getContext(),0));
        text_head.setText(headText);
        addView(text_head);

    }
	/*
	public void setHead(int resid)
	{

	}*/

    public void setHeadColor(int color)
    {
        this.headColor = color;
    }

    //添加一个switch(开关设置)
    public void addSwitch(String title,String info,final String className)
    {
        LinearLayout.LayoutParams layoutParams=null;
        final SwitchCompat check = new SwitchCompat(getContext());
        check.setChecked(getBoolean(className));
        TextView text_title = new TextView(getContext());
        text_title.setTextSize(18);
        text_title.setText(title);
        text_title.setSingleLine(true);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));

        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));
        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);
        layout_main.addView(check);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addView(layout_main, layoutParams);
        OnClickListener click = new OnClickListener()
        {

            @Override
            public void onClick(View p1)
            {
                if(p1 instanceof ViewGroup)
                {
                    check.toggle();
                }
                else
                {

                }
                setBoolean(className,check.isChecked());
            }


        };
        layout_main.setClickable(true);
        layout_main.setOnClickListener(click);
        check.setOnClickListener(click);

    }

    public void addSwitch(int title_id, int info_id, String className)
    {
        this.addSwitch(getContext().getString(title_id), getContext().getString(info_id), className);
    }

    //添加一个checkbox
    public void addCheck(String title,String info,final String className)
    {
        LinearLayout.LayoutParams layoutParams=null;
        final AppCompatCheckBox check = new AppCompatCheckBox(getContext());
        check.setChecked(getBoolean(className));
        TextView text_title = new TextView(getContext());
        text_title.setTextSize(18);
        text_title.setText(title);
        text_title.setSingleLine(true);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));
        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));
        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);
        layout_main.addView(check);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addView(layout_main, layoutParams);
        OnClickListener click = new OnClickListener()
        {

            @Override
            public void onClick(View p1)
            {
                if(p1 instanceof ViewGroup)
                {
                    check.toggle();
                }
                else
                {

                }
                setBoolean(className,check.isChecked());
            }


        };
        layout_main.setClickable(true);
        layout_main.setOnClickListener(click);
        check.setOnClickListener(click);

    }

    public void addCheck(int title_id,int info_id,String className)
    {
        this.addCheck(getContext().getString(title_id), getContext().getString(info_id), className);
    }


    //添加一个seekbar(数值设置)
    public void addSeek(String title,String info,final String className)
    {
        LinearLayout.LayoutParams layoutParams=null;

        TextView text_title = new TextView(getContext());
        text_title.setSingleLine(true);
        text_title.setTextSize(18);
        text_title.setText(title);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));
        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));

        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);
        //layout_land.addView(check);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout_main.setMinimumHeight(DisplayUtil.dip2px(getContext(),80));
        addView(layout_main, layoutParams);

        layout_main.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                Context context = p1.getContext();
                final SeekBar seekbar = new SeekBar(context);
                seekbar.setProgress(getInt(className));

                AlertDialog.Builder builder= new AlertDialog.Builder(context);
                String title = "设置数值";

                //edit.setPadding(DisplayUtil.dip2px(context,16),8,DisplayUtil.dip2px(context,16),8);
                //edit.setHint(R.string.edit_save_hint);
//	builder.setIcon(R.drawable.icon);
                builder.setTitle(title);
                builder.setView(seekbar,DisplayUtil.dip2px(context,8),8,DisplayUtil.dip2px(context,8),8);

                builder.setPositiveButton(context.getString(R.string.accept), new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        setNum(className,seekbar.getProgress());


                    }
                });
                builder.setNegativeButton(context.getString(R.string.refused), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {

                    }
                }).show();
            }


        });
    }

    public void addSeek(int title_id, int info_id, String className)
    {
        this.addSeek(getContext().getString(title_id), getContext().getString(info_id), className);
    }

    //添加一个编辑框(文本设置)
    public void addEdit(String title,String info,final String className)
    {
        LinearLayout.LayoutParams layoutParams=null;

        TextView text_title = new TextView(getContext());
        text_title.setSingleLine(true);
        text_title.setTextSize(18);
        text_title.setText(title);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));
        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));

        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);
        //layout_land.addView(check);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout_main.setMinimumHeight(DisplayUtil.dip2px(getContext(),80));
        addView(layout_main, layoutParams);

        layout_main.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                Context context = p1.getContext();
                final AppCompatEditText edit = new AppCompatEditText(context);
                edit.setText(getString(className));
                if(getClassTypeName(className).equals("int"))
                {
                    edit.setInputType( EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
                }

                AlertDialog.Builder builder= new AlertDialog.Builder(context);
                String title = "设置文本";

                //edit.setPadding(DisplayUtil.dip2px(context,16),8,DisplayUtil.dip2px(context,16),8);
                //edit.setHint(R.string.edit_save_hint);
//	builder.setIcon(R.drawable.icon);
                builder.setTitle(title);
                builder.setView(edit,DisplayUtil.dip2px(context,8),8,DisplayUtil.dip2px(context,8),8);

                builder.setPositiveButton(context.getString(R.string.accept), new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        if(getClassTypeName(className).equals("int"))
                        {
                            setInt(className,Str.atoi(edit.getText().toString()));
                        }
                        else
                            setString(className,edit.getText().toString());

                    }
                });
                builder.setNegativeButton(context.getString(R.string.refused), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {

                    }
                }).show();
            }


        });
    }

    public void addEdit(int title_id,int info_id, String className)
    {
        this.addEdit(getContext().getString(title_id), getContext().getString(info_id), className);
    }
    //添加一个颜色选择(颜色设置)
//    public void addColor(String title,String info,final String className)
//    {
//        LinearLayout.LayoutParams layoutParams=null;
//
//        TextView text_title = new TextView(getContext());
//        text_title.setSingleLine(true);
//        text_title.setTextSize(18);
//        text_title.setText(title);
//        TextView text_info = new TextView(getContext());
//        text_info.setTextSize(14);
//        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));
//        text_info.setText(info);
//        text_info.setSingleLine(true);
//        if(info==null)
//            text_info.setVisibility(View.GONE);
//        else if(info.length()==0)
//            text_info.setVisibility(View.GONE);
//        LinearLayout layout_main = new LinearLayout(getContext());
//        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
//        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
//        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);
//
//        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));
//
//        layout_main.setGravity(Gravity.CENTER_VERTICAL);
//        int padding = DisplayUtil.dip2px(getContext(),16);
//        layout_main.setPadding(padding,padding,padding,padding);
//        //纵向
//        LinearLayout layout = new LinearLayout(getContext());
//        layout.setOrientation(LinearLayout.VERTICAL);
//        layout.addView(text_title);
//        layout.addView(text_info);
//        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
//        layout_main.addView(layout,layoutParams);
//        final CirColorView colorView = new CirColorView(getContext());
//        colorView.setColor(getInt(className));
//        colorView.setCirSize(DisplayUtil.dip2px(getContext(),2));
//
//        layoutParams = new LinearLayout.LayoutParams(DisplayUtil.dip2px(getContext(),36), DisplayUtil.dip2px(getContext(),32));
//        layout_main.addView(colorView,layoutParams);
//        //layout_land.addView(check);
//        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        layout_main.setMinimumHeight(DisplayUtil.dip2px(getContext(),80));
//        addView(layout_main, layoutParams);
//
//        OnClickListener listener =  new OnClickListener()
//        {
//
//            @Override
//            public void onClick(View p1)
//            {
//                // TODO: Implement this method
//                Context context = p1.getContext();
//                final ColorPickerSeek view = new ColorPickerSeek(context);
//                view.setColor(getInt(className));
//                AlertDialog.Builder builder= new AlertDialog.Builder(context);
//                String title = "设置颜色";
//
//                //edit.setPadding(DisplayUtil.dip2px(context,16),8,DisplayUtil.dip2px(context,16),8);
//                //edit.setHint(R.string.edit_save_hint);
////	builder.setIcon(R.drawable.icon);
//                builder.setTitle(title);
//                builder.setView(view,DisplayUtil.dip2px(context,8),8,DisplayUtil.dip2px(context,8),8);
//
//                builder.setPositiveButton(context.getString(R.string.accept), new DialogInterface.OnClickListener()
//                {
//                    public void onClick(DialogInterface dialog, int whichButton)
//                    {
//                        setNum(className, view.getColor());
//                        colorView.setColor(view.getColor());
//                    }
//                });
//                builder.setNegativeButton(context.getString(R.string.refused), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int whichButton)
//                    {
//
//                    }
//                }).show();
//            }
//
//
//        };
//        layout_main.setOnClickListener(listener);
//        colorView.setOnClickListener(listener);
//
//    }

//    public void addColor(int title_id,int info_id,String className)
//    {
//        this.addColor(getContext().getString(title_id), getContext().getString(info_id),className);
//    }

    //添加一个图片选择(图片设置)
    public void addPicture(String title,String info,String var)
    {

    }
    //添加一个文件选择(文件设置)
    public void addFile(String title,String info,String var)
    {

    }
    //添加一个选项(带右侧箭头)
    public void addItem(String title,String info, View.OnClickListener click)
    {
        LinearLayout.LayoutParams layoutParams=null;


        TextView text_title = new TextView(getContext());
        text_title.setTextSize(18);
        text_title.setText(title);
        text_title.setSingleLine(true);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));

        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));
        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);

        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        addView(layout_main, layoutParams);

        layout_main.setClickable(true);

        layout_main.setOnClickListener(click);


    }
    //添加一个列表选择(单选)(listDialog)
    public void addList(String title, String info, String[] arrays, String className)
    {
        LinearLayout.LayoutParams layoutParams=null;

        TextView text_title = new TextView(getContext());
        text_title.setSingleLine(true);
        text_title.setTextSize(18);
        text_title.setText(title);
        TextView text_info = new TextView(getContext());
        text_info.setTextSize(14);
        text_info.setTextColor(ColorUtil.getAlphaColor(ColorUtil.getColorAttr(getContext(), android.R.attr.textColorSecondary), 128));
        text_info.setText(info);
        text_info.setSingleLine(true);
        if(info==null)
            text_info.setVisibility(View.GONE);
        else if(info.length()==0)
            text_info.setVisibility(View.GONE);
        LinearLayout layout_main = new LinearLayout(getContext());
        ColorDrawable drawable = new ColorDrawable(0x00ffffff);
        ColorDrawable drawable_pass = new ColorDrawable(0x30ffffff);
        ColorDrawable drawable_focus = new ColorDrawable(0x20ffffff);

        ViewTool.setBackgroundDrawable(layout_main,addStateDrawable(getContext(),drawable,drawable_pass,drawable_focus));

        layout_main.setGravity(Gravity.CENTER_VERTICAL);
        int padding = DisplayUtil.dip2px(getContext(),16);
        layout_main.setPadding(padding,padding,padding,padding);
        //纵向
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(text_title);
        layout.addView(text_info);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,10);
        layout_main.addView(layout,layoutParams);
        //layout_land.addView(check);
        layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout_main.setMinimumHeight(DisplayUtil.dip2px(getContext(),80));
        addView(layout_main, layoutParams);

        layout_main.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                Context context = p1.getContext();
                AlertDialog.Builder builder= new AlertDialog.Builder(context);
                builder.setTitle(title);
                builder.setItems(arrays, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        setString(className, arrays[i]);
                    }
                });

                builder.setNegativeButton(context.getString(R.string.refused), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {

                    }
                }).show();
            }


        });
    }
    //添加一个Activity跳转选项(点击之后跳转)
    public void addActivityItem(String title,String info,String pageName)
    {

    }
    //添加一个网址选项(点击之后跳转到内部浏览器)
    public void addUrl(String title,String info,String url)
    {

    }

    private String getClassTypeName(String className)
    {
        try
        {
            //拆分类和名字
            int end=Str.strrchr(className.toCharArray(),'.');
            String class_pack= className.substring(0,end);
            String class_name= className.substring(end+1);
            //获取Bean类的INT_VALUE字段

            Field field = Class.forName(class_pack).getField(class_name);


            return field.getType().getName();
            //System.out.println(Bean.INT_VALUE);
        }
        catch (ClassNotFoundException e)
        {}
        catch (NoSuchFieldException e)
        {}
        catch (IllegalArgumentException e)
        {}
        return null;
    }

    private void setInt(String className,int num)
    {
        //System.out.println(Bean.INT_VALUE);
        try
        {
            //拆分类和名字
            int end=Str.strrchr(className.toCharArray(),'.');
            String class_pack= className.substring(0,end);
            String class_name= className.substring(end+1);
            //获取Bean类的INT_VALUE字段

            Field field = Class.forName(class_pack).getField(class_name);


            //将字段的访问权限设为true：即去除private修饰符的影响
            field.setAccessible(true);
            /*去除final修饰符的影响，将字段设为可修改的*/
	/*
	Field modifiersField = Field.class.getDeclaredField("modifiers");
	modifiersField.setAccessible(true);


			modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
		*/

            //把字段值设为200
            field.set(null, num);
            //System.out.println(Bean.INT_VALUE);
        }
        catch (ClassNotFoundException e)
        {}
        catch (NoSuchFieldException e)
        {}
        catch (IllegalAccessException e)
        {}
        catch (IllegalArgumentException e)
        {}
    }

    private void setString(String className,String text)
    {
        //System.out.println(Bean.INT_VALUE);
        try
        {
            //拆分类和名字
            int end=Str.strrchr(className.toCharArray(),'.');
            String class_pack= className.substring(0,end);
            String class_name= className.substring(end+1);
            //获取Bean类的INT_VALUE字段

            Field field = Class.forName(class_pack).getField(class_name);


            //将字段的访问权限设为true：即去除private修饰符的影响
            field.setAccessible(true);
            /*去除final修饰符的影响，将字段设为可修改的*/
			/*
			Field modifiersField = Field.class.getDeclaredField("modifiers");
			modifiersField.setAccessible(true);


			modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
*/

            //把字段值设为200
            field.set(null, text);
            //System.out.println(Bean.INT_VALUE);
        }
        catch (ClassNotFoundException e)
        {}
        catch (NoSuchFieldException e)
        {}
        catch (IllegalAccessException e)
        {}
        catch (IllegalArgumentException e)
        {}
    }

    private void setBoolean(String className,boolean check)
    {
        //System.out.println(Bean.INT_VALUE);
        try
        {
            //拆分类和名字
            int end=Str.strrchr(className.toCharArray(),'.');
            String class_pack= className.substring(0,end);
            String class_name= className.substring(end+1);
            //获取Bean类的INT_VALUE字段

            Field field = Class.forName(class_pack).getField(class_name);


            //将字段的访问权限设为true：即去除private修饰符的影响
            field.setAccessible(true);
            /*去除final修饰符的影响，将字段设为可修改的*/
			/*
			 Field modifiersField = Field.class.getDeclaredField("modifiers");
			 modifiersField.setAccessible(true);


			 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
			 */

            //把字段值设为200
            field.setBoolean(null, check);
            //System.out.println(Bean.INT_VALUE);
        }
        catch (ClassNotFoundException e)
        {}
        catch (NoSuchFieldException e)
        {}
        catch (IllegalAccessException e)
        {}
        catch (IllegalArgumentException e)
        {}
    }

    private void setNum(String className,int num)
    {
        //System.out.println(Bean.INT_VALUE);
        try
        {
            //拆分类和名字
            int end=Str.strrchr(className.toCharArray(),'.');
            String class_pack= className.substring(0,end);
            String class_name= className.substring(end+1);
            //获取Bean类的INT_VALUE字段

            Field field = Class.forName(class_pack).getField(class_name);


            //将字段的访问权限设为true：即去除private修饰符的影响
            field.setAccessible(true);
            /*去除final修饰符的影响，将字段设为可修改的*/
			/*
			 Field modifiersField = Field.class.getDeclaredField("modifiers");
			 modifiersField.setAccessible(true);


			 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
			 */

            //把字段值设为200
            field.setInt(null, num);
            //System.out.println(Bean.INT_VALUE);
        }
        catch (ClassNotFoundException e)
        {}
        catch (NoSuchFieldException e)
        {}
        catch (IllegalAccessException e)
        {}
        catch (IllegalArgumentException e)
        {}
    }

    //r文件
    int getInt(String className)
    {
        int num=0;
        //拆分类和名字
        int end=Str.strrchr(className.toCharArray(),'.');
        String class_pack= className.substring(0,end);
        String class_name= className.substring(end+1);

        try
        {
            Class r = Class.forName(class_pack);
            Object obj=null;
            try
            {
                obj= r.newInstance();
                //obj = new R.drawable();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }

            try
            {
                Field id = obj.getClass().getField(class_name);
                try
                {
                    num = id.getInt(obj);
                }
                catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }
                catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }
            }
            catch (NoSuchFieldException e)
            {
                e.printStackTrace();
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return num;
    }

    boolean getBoolean(String className)
    {
        boolean num=false;
        //拆分类和名字
        int end=Str.strrchr(className.toCharArray(),'.');
        String class_pack= className.substring(0,end);
        String class_name= className.substring(end+1);

        try
        {
            Class r = Class.forName(class_pack);
            Object obj=null;
            try
            {
                obj= r.newInstance();
                //obj = new R.drawable();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }

            try
            {
                Field id = obj.getClass().getField(class_name);
                try
                {
                    num = id.getBoolean(obj);
                }
                catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }
                catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }
            }
            catch (NoSuchFieldException e)
            {
                e.printStackTrace();
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return num;
    }

    String getString(String className)
    {
        String num=null;
        //拆分类和名字
        int end=Str.strrchr(className.toCharArray(),'.');
        String class_pack= className.substring(0,end);
        String class_name= className.substring(end+1);

        try
        {
            Class r = Class.forName(class_pack);
            Object obj=null;
            try
            {
                obj= r.newInstance();
                //obj = new R.drawable();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }

            try
            {
                Field id = obj.getClass().getField(class_name);

                try
                {
                    if(id.getType().getName().equals("int"))
                    {
                        num = ""+id.getInt(obj);
                    }
                    else
                        num = (String)id.get(obj);
                }
                catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                }
                catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }
            }
            catch (NoSuchFieldException e)
            {
                e.printStackTrace();
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return num;
    }





    private StateListDrawable addStateDrawable(Context context,Drawable normal , Drawable pressed, Drawable focus){

        StateListDrawable sd =new StateListDrawable();
        //注意该处的顺序，只要有一个状态与之相配，背景就会被换掉
//所以不要把大范围放在前面了，如果sd.addState(new[]{},normal)放在第一个的话，就没有什么效果了
        sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
        sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
        sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
        sd.addState(new int[]{android.R.attr.state_focused}, focus);

        sd.addState(new int[]{android.R.attr.state_enabled}, normal);
        sd.addState(new int[]{}, normal);
        return sd;
    }

    private StateListDrawable addStateDrawable(Context context,int idNormal,int idPressed,int idFocused){

        StateListDrawable sd =new StateListDrawable();
        Drawable normal = idNormal ==-1?null: context.getResources().getDrawable(idNormal);
        Drawable pressed = idPressed ==-1?null: context.getResources().getDrawable(idPressed);
        Drawable focus = idFocused ==-1?null: context.getResources().getDrawable(idFocused);
//注意该处的顺序，只要有一个状态与之相配，背景就会被换掉
//所以不要把大范围放在前面了，如果sd.addState(new[]{},normal)放在第一个的话，就没有什么效果了
        sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
        sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus);
        sd.addState(new int[]{android.R.attr.state_pressed}, pressed);
        sd.addState(new int[]{android.R.attr.state_focused}, focus);

        sd.addState(new int[]{android.R.attr.state_enabled}, normal);
        sd.addState(new int[]{}, normal);
        return sd;
    }

    /** 设置Selector。 */
    public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,
                                                int idUnable) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
        Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
        Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_focused }, focused);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[] {}, normal);
        return bg;
    }

    /** 设置不同状态时的颜色。
     参数：正常 按下 焦点 启用
     */
    private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
        int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
        int[][] states = new int[6][];
        states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
        states[2] = new int[] { android.R.attr.state_enabled }; //启用控件
        states[3] = new int[] { android.R.attr.state_focused }; //获得焦点
        states[4] = new int[] { android.R.attr.state_window_focused }; //平常
        states[5] = new int[] {};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    //获取colorAccept
    int getColorAccent()
    {
        int defaultColor = 0xFF000000;
        int[] attrsArray = {android.support.v7.appcompat.R.attr.colorAccent};
        TypedArray typedArray = getContext().obtainStyledAttributes(attrsArray);
        int accentColor = typedArray.getColor(0, defaultColor);

// don't forget the resource recycling
        typedArray.recycle();
        return accentColor;
    }

}
